package Chapter14.Homework;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @author 杨森
 * @date 2022年10月27日 20:09
 */
public class MapExercise {
    public static void main(String[] args) {
        Map map = new HashMap();

        map.put(1,new Person("jack",17000,1));
        map.put(2,new Person("tom",20000,2));
        map.put(3,new Person("sara",21000,3));

//        输出工资大于18000的
//        增强for循环
        Set keys = map.keySet();
        for (Object key :keys) {
            Person person = (Person) map.get(key);
            if(person.getSal()>=18000){
                System.out.println(person);
            }
        }

//        迭代器
        Iterator it = map.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            Person person = (Person) entry.getValue();
            if (person.getSal()>18000){
                System.out.println(person);
            }
        }

    }
}

class Person{
    private String name;
    private double sal;
    private int id;

    public Person(String name, double sal,int id) {
        this.name = name;
        this.sal = sal;
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSal() {
        return sal;
    }

    public void setSal(double sal) {
        this.sal = sal;
    }

    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("Person{");
        sb.append("name='").append(name).append('\'');
        sb.append(", sal=").append(sal);
        sb.append(", id=").append(id);
        sb.append('}');
        return sb.toString();
    }
}

